﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.CRM.Library.Views;
using nvn.Library.Patterns;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Utility;

namespace Hutech.CRM.Web.Controls.QuanLyDanhMucThongTin
{
    public partial class AddNewDanhMucControl : System.Web.UI.UserControl, IAddNewDanhMucView
    {
        #region Cac su kien uy thac
        private DanhMucThongTin _objecOnDelaga;

        public DanhMucThongTin ObjecOnDelaga
        {
            get { return _objecOnDelaga; }
            set { _objecOnDelaga = value; }
        }
        public event EventHandler<OneParameterEventAgrs<string>> ReloadData;
        protected void onReloadData(string paramater)
        {
            if (ReloadData != null)
            {
                ReloadData(this, new OneParameterEventAgrs<string>(paramater));
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.Visible = false;
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.InitView();
            this.ImageButtonSave.Click += new EventHandler(ImageButtonSave_Click);
            this.ImageButtonCancel.Click += new EventHandler(ImageButtonCancel_Click);
        }


        void ImageButtonCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.onReloadData(ConstantVariable.COMMAND_CANCEL);
        }

        void ImageButtonSave_Click(object sender, EventArgs e)
        {
            DanhMucThongTin danhMuc = new DanhMucThongTin();
            try
            {
                danhMuc.DanhMucName = txtTen.Text;
                danhMuc.DanhMucID = txtVietTat.Text;
                danhMuc.LoaiDanhMucGuid = Guid.Parse(HiddenFieldLoaiDanhMucGuid.Value);
                danhMuc.LoaiDanhMucName = HiddenFieldLoaiDanhMucName.Value;
                danhMuc.IsActive = Convert.ToBoolean(chkIsActive.Checked);
            }
            catch (Exception)
            {
                ErrorMessage = "Vui lòng refresh lại trang";
                return;
            }
            OnSave_Click(danhMuc);
            txtTen.Text = string.Empty;
            txtVietTat.Text = string.Empty;
            onReloadData(ConstantVariable.COMMAND_RELOAD);
        }
        public string ErrorMessage
        {
            set
            {
                if (value != null)
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Show", ShowError.Show(value), false);
            }
        }
        public event EventHandler<SaveEventAgrs<DanhMucThongTin>> Save_Click;
        private void OnSave_Click(DanhMucThongTin myType)
        {
            if (Save_Click != null)
                Save_Click(this, new SaveEventAgrs<DanhMucThongTin>(myType));
        }
        public event EventHandler Cancel_Click;

        public event EventHandler<EventArgs> FristLoadEventHandler;
        private void OnFristLoadEventHandler()
        {
            if (FristLoadEventHandler != null)
                FristLoadEventHandler(this, new EventArgs());
        }

        internal void Show(DanhMucThongTin objectOnDelega)
        {
            try
            {
                HiddenFieldLoaiDanhMucGuid.Value = objectOnDelega.LoaiDanhMucGuid.ToString();
                HiddenFieldLoaiDanhMucName.Value = objectOnDelega.LoaiDanhMucName.ToString();
                this.Visible = true;
            }
            catch (Exception)
            {
                this.ErrorMessage = "Vui lòng refresh lại trang";
            }
        }


        public void CallBack()
        {
            DanhMucThongTin item = new DanhMucThongTin();
            try
            {
                item.DanhMucGuid = Guid.Parse(HiddenFieldLoaiDanhMucGuid.Value);
                item.DanhMucName = HiddenFieldLoaiDanhMucName.Value;
                _objecOnDelaga = item;
                this.onReloadData(ConstantVariable.COMMAND_ENABLE);
            }
            catch (Exception)
            {
                this.ErrorMessage = "Vui lòng refresh lại trang";
            }
        }
    }
}